﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using ClassLib.DBAuthentication.DataProvider;
using ClassLib.Classes.Common;
using ClassLib.Froms.Connection;
using DevExpress.XtraSplashScreen;
using ISumen;
using Sumen.Business;
using Sumen.Business.Model;
using Sumen.Business.Model.Presenter.Actor;
using Sumen.Business.Model.Presenter.Sys;

namespace Sumen_Stock.LookAndFeel.Sumen_System
{
    public partial class FrmDangNhap : DevExpress.XtraEditors.XtraForm
    {
        Chuoi.EncryptDecrypt ed = new Chuoi.EncryptDecrypt();

        private UserPresenter _userPresenter;
        bool bSucc = false;
        string vPassword = string.Empty;

        Main objFrmMain;
        private string taikhoan = global::Sumen_Stock.Properties.Settings.Default.User + string.Empty;
        public FrmDangNhap()
        {
            InitializeComponent();
            _userPresenter = new UserPresenter();
            if (taikhoan == "")
                checkEdit1.CheckState = CheckState.Unchecked;
            else
                checkEdit1.CheckState = CheckState.Checked;
            if (checkEdit1.Checked)
                txtTaiKhoan.Text = taikhoan;
        }

        private void FrmDangNhap_Load(object sender, EventArgs e)
        {
            txtTaiKhoan.Focus();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (KiemTraHopLe() == true)
            {
                this.Enabled = false;
                SplashScreenManager.ShowForm(typeof(WaitForm1));
                user user = _userPresenter.GetById(txtTaiKhoan.Text.Trim());
                if (user != null)
                {
                    vPassword = ClsPublic.EncryptSHA512(txtMatKhau.Text.Trim());
                    if (vPassword == user.User_Password)
                    {
                        if (user.User_Actived == false)
                        {
                            ClsBaoLoi.ThongTin("Tài khoản đã bị khóa.");
                            this.Enabled = true;
                            SplashScreenManager.CloseForm();
                            return;
                        }
                        if (checkEdit1.Checked)
                        {
                            global::Sumen_Stock.Properties.Settings.Default.User = txtTaiKhoan.Text.Trim();
                            global::Sumen_Stock.Properties.Settings.Default.Save();
                        }
                        else
                        {
                            global::Sumen_Stock.Properties.Settings.Default.User = "";
                            global::Sumen_Stock.Properties.Settings.Default.Save();
                        }

                        SplashScreenManager.CloseForm();
                        bSucc = true;
                        this.Hide();
                        ClsPublic.User = user;
                        if (objFrmMain == null || objFrmMain.IsDisposed)
                            //objFrmMain = new FrmMain_CTU();
                            objFrmMain = new Main();
                        objFrmMain.Show();
                        objFrmMain.Activate();

                    }
                    else
                    {
                        ClsBaoLoi.ThongTin("Mật khẩu không hợp lệ.\nBạn vui lòng kiểm tra lại.");
                        vPassword = string.Empty;
                        this.Enabled = true;
                        SplashScreenManager.CloseForm();
                    }
                }
                else
                {
                    ClsBaoLoi.Loi("Tài khoản không tồn tại.\nBạn vui lòng kiểm tra lại.");
                    txtTaiKhoan.Focus();
                    vPassword = string.Empty;
                    this.Enabled = true;
                    SplashScreenManager.CloseForm();
                }

            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtMatKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnDangNhap_Click(btnDangNhap, e);
            }
        }

        private void FrmDangNhap_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bSucc == false)
                Application.Exit();
        }

        private bool KiemTraHopLe()
        {
            if (txtTaiKhoan.Text.Trim() == "")
            {
                ClsBaoLoi.ThongTin("Tài khoản không thể rỗng.\nBạn vui lòng nhập tài khoản.");
                txtTaiKhoan.Focus();
                return false;
            }

            if (txtMatKhau.Text.Trim() == "")
            {
                ClsBaoLoi.ThongTin("Mật khẩu không thể rỗng.\nBạn vui lòng nhập mật khẩu.");
                txtMatKhau.Focus();
                return false;
            }

            return true;
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            FrmCauHinhCSDL obj = new FrmCauHinhCSDL();
            obj.Show();
        }


    }
}
